﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using KPIS.GERP.GIMS.BLL;
using KPIS.GERP.GIMS.IBLL;
using KPIS.GERP.GIMS.MODEL;
using System.Data;

namespace KPIS.GERP.GIMS.APP.APP_UI.Form
{
    public partial class ICM_Master_dispense_types : System.Web.UI.Page
    {
        Common.Main cn = new Common.Main();
        CommandControl cmdFac = new CommandControl();
        IBLLMasterUnit bll = new MasterUnitBLL();
        private DispenseTypeBLL disTypeBLL = new DispenseTypeBLL();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadDataGrid();
            }
        }

        public void LoadDataGrid()
        {
            DataSet ListDataCommand = disTypeBLL.GetDispenseType(cn.ConvertNullInt(""));
            DataTable table = ListDataCommand.Tables[0];        
            DataGrid.DataSource = ListDataCommand.Tables[0];
            DataGrid.DataBind();
            cmdFac.GridCommand(panelList, panelGrid, panelCommand);
        }

        public void GetKeyValue(string seq)
        {
            if (seq.Length > 0)
            {
                DataSet ListDataCommand = disTypeBLL.GetDispenseType(cn.ConvertNullInt(seq));
                DataTable table = ListDataCommand.Tables[0];
                txtName.Text = table.Rows[0]["Name"].ToString();
                txtDescr.Text = table.Rows[0]["remark"].ToString();

                cmdFac.ViewCommand(panelList, panelGrid, panelCommand);
            }
        }

        protected void AddNew_Click(object sender, EventArgs e)
        {
            cmdFac.AddNewCommand(panelList, panelGrid, panelCommand);
        }

        protected void Edit_Click(object sender, EventArgs e)
        {
            cmdFac.EditCommand(panelList, panelGrid, panelCommand);
        }

        protected void Delete_Click(object sender, EventArgs e)
        {
            if (panelGrid.Visible)
            {
                foreach (GridDataItem data in DataGrid.MasterTableView.GetSelectedItems())
                {
                    disTypeBLL.Delete(Convert.ToInt32(data.OwnerTableView.DataKeyValues[data.ItemIndex]["Sequence"]), Convert.ToInt32(Session["EmpSeq"]));
                }
            }
            
            LoadDataGrid();
        }

        protected void AddNewSave_Click(object sender, EventArgs e)
        {
            if (ValtxtName.IsValid == false)
            {
                return;
            }

            try
            {
                disTypeBLL.Insert(txtName.Text, txtDescr.Text, Convert.ToInt32(Session["EmpSeq"]));
            }
            catch
            {
                RadManager1.Alert(SystemInfo._InsertError);
                return;
            }
            LoadDataGrid();
        }

        protected void EditSave_Click(object sender, EventArgs e)
        {

            if (ValtxtName.IsValid == false)
            {
                return;
            }

            try
            {
                disTypeBLL.Update(Convert.ToInt32(hdSeq.Value), txtName.Text, txtDescr.Text, Convert.ToInt32(Session["EmpSeq"]));
            }
            catch
            {
                RadManager1.Alert(SystemInfo._InsertError);
                return;
            }
            cmdFac.ViewCommand(panelList, panelGrid, panelCommand);
        }

        protected void Cancel_Click(object sender, EventArgs e)
        {
            cmdFac.CancelCommand(panelList, panelGrid, panelCommand);
        }

        protected void ShowGrid_Click(object sender, EventArgs e)
        {
            LoadDataGrid();
        }

        protected void DataGrid_ItemCommand(object source, GridCommandEventArgs e)
        {
            LoadDataGrid();
        }

        protected void RadManager1_AjaxRequest(object sender, AjaxRequestEventArgs e)
        {
            GetKeyValue(e.Argument);
        }

        protected void OnAjaxRequest(object sender, AjaxRequestEventArgs e)
        {
            RadAjaxPanel1.Controls.Add(Page.LoadControl("../UserControl/Personal_Data.ascx"));
        }

        protected void clearFilter_Click(object sender, EventArgs e)
        {
            foreach (GridColumn column in DataGrid.MasterTableView.Columns)
            {
                column.CurrentFilterFunction = GridKnownFunction.NoFilter;
                column.CurrentFilterValue = string.Empty;
            }
            DataGrid.MasterTableView.FilterExpression = string.Empty;
            LoadDataGrid();
        }
    }
}
